package com.fare.taluo.core.domain.queue.message;

import javax.persistence.Column;
import javax.persistence.Table;

import org.hibernate.annotations.Where;

import com.dayatang.domain.InstanceFactory;
import com.fare.taluo.core.domain.queue.QueueBaseEntity;
import com.fare.taluo.core.domain.queue.QueueBaseRepository;
import com.fare.taluo.core.domain.queue.message.repository.impl.QueueEmailMessageRepositoryImpl;
import com.fare.taluo.message.SendEmail;
@javax.persistence.Entity
@Table(name="que_email_message")
@Where(clause="flg_deleted=0")
public class QueueEmailMessageEntity extends QueueBaseEntity<QueueEmailMessageEntity>
			{

	/**
	 * 
	 */
	private static final long serialVersionUID = -2667638506880361227L;
	/**
	 * 发送人地址
	 */
	@Column(name = "FROM_ADDRESS")
	private String fromAddress;
	/**
	 * 接收人地址，多个以分号分隔
	 */
	@Column(name = "TO_ADDRESS")
	private String toAddress;
	/**
	 * 抄送人地址，多个以分号分隔
	 */
	@Column(name = "COPYTO_ADDRESS")
	private String copyToAddress;
	/**
	 * 主题 
	 */
	@Column(name = "SUBJECT")
	private String subject;
	/**
	 * 内容
	 */
	@Column(name = "BODY")
	private String body;
	/**
	 * 附件全路径，多个附件以分号分隔
	 */
	@Column(name = "ATTACH_PATH")
	private String attachPath;
	
	 
	public String getFromAddress() {
		return fromAddress;
	}
	public void setFromAddress(String fromAddress) {
		this.fromAddress = fromAddress;
	}
	public String getToAddress() {
		return toAddress;
	}
	public void setToAddress(String toAddress) {
		this.toAddress = toAddress;
	}
	public String getCopyToAddress() {
		return copyToAddress;
	}
	public void setCopyToAddress(String copyToAddress) {
		this.copyToAddress = copyToAddress;
	}
	public String getSubject() {
		return subject;
	}
	public void setSubject(String subject) {
		this.subject = subject;
	}
	public String getBody() {
		return body;
	}
	public void setBody(String body) {
		this.body = body;
	}
	public String getAttachPath() {
		return attachPath;
	}
	public void setAttachPath(String attachPath) {
		this.attachPath = attachPath;
	}
	@Override
	protected QueueBaseRepository<QueueEmailMessageEntity> getEntityRepository() {
		return InstanceFactory.getInstance(QueueEmailMessageRepositoryImpl.class);
	}
	
	@Override
	public QueueEmailMessageEntity createNextTask(Long id) throws Exception {
		if(logger.isDebugEnabled()){
			logger.debug("邮件队列，不需要周期性创建下一个任务");
		}
		return null;
	}
	@Override
	public void executeTask(Long id) throws Exception {
		QueueEmailMessageEntity entity = getEntityRepository().get(this.getClass(), id);
		if(entity != null){
			new SendEmail()
			.setTo(entity.getToAddress())
			.setFileAffix(entity.getAttachPath())
			.setSubject(entity.getSubject())
			.setBody(entity.getBody())
			.send();
		}
	}
}
